<template>

  <el-container class="home-container">
    <!-- 头部区 -->
    <el-header>
      <div>
        <!--        <img src="../assets/logome.png" alt="">-->
        <img src="../assets/12300.gif" alt="">
        <span>市场部后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 左侧 -->
      <el-aside :width="isCollapse ? '64px' : '200px' ">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!--        侧边栏区域-->
        <el-menu
          background-color='rgb(1, 60, 87)'
          text-color="#fff"
          active-text-color="#2ca8f5" unique-opened :collapse="isCollapse" :collapse-transition="false" router
          :default-active="activePath">
          <!--          一级菜单-->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- for循环要指定不同的key index也是 不然一个展开全部展开 而且得是字符串格式 -->
            <!--一级菜单模板区-->
            <template slot="title">
              <!-- 一级菜单图标-->
              <i :class="iconObj[item.id]"></i>
              <!--文本-->
              <span>{{ item.authName }}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item :index=" '/'+subItem.path + ''" v-for="subItem in item.children" :key="subItem.id"
                          @click="savNavState( '/'+subItem.path + '')">
              <template slot="title">
                <!--一级菜单图标-->
                <i class="el-icon-location"></i>
                <!--              文本-->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>
      <!-- 右侧 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      isCollapse: false,
      activePath: '',  //点击菜单列表地址的本地存储数据
    }
  },
  created() {
    this.getMenuList(),
      this.activePath = window.sessionStorage.getItem('activePath')

  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有菜单数据
    async getMenuList() {
      const {data: res} = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.message)
      }
      this.$message.success('获取数据成功！')
      this.menulist = res.data
      // console.log(res)
    },
    // 点击按钮切换菜单展叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 点击菜单的时候的触发事件--报错裂变的点击状态
    savNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background: rgb(3, 64, 93);
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: whitesmoke;
  font-size: 20px;

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 10px;

    }

    img {
      width: 65px;
      height: 65px;
      border-radius: 59%;
    }
  }
}

.el-aside {
  background: rgb(1, 60, 87);

  .el-menu {
    border-right: none;
  }
}

.el-main {
  background: #fbfcfc;
}

.home-container {
  height: 100%;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background: rgb(65, 109, 131);
  font-size: 10px;
  line-height: 24px;
  color: white;
  text-align: center;
  letter-spacing: 0.2rem;
  cursor: pointer;

}

.toggle-button:hover {
  background: rgb(76, 127, 152);
}

</style>
